package com.pms.controller;

import com.pms.common.PageBean;
import com.pms.pojo.BizAskLeave;
import com.pms.result.Result;
import com.pms.service.BizAskLeaveService;
import com.pms.util.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/leave")
@Slf4j
public class BizAskLeaveController {

    @Autowired
    private BizAskLeaveService bizAskLeaveService;

    @GetMapping
    public Result<PageBean<BizAskLeave>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name,
            String leaveType,
            Integer status,
            String beginTime,
            String endTime) {
        log.info("查询请假记录，参数：page={}, pageSize={}, name={}, leaveType={}, status={}, beginTime={}, endTime={}", page, pageSize, name, leaveType, status, beginTime, endTime);
        PageBean<BizAskLeave> pageBean = bizAskLeaveService.getLeaveList(
                name, leaveType, status, beginTime, endTime, page, pageSize);
        return Result.success(pageBean);
    }

    @PutMapping("/approve/{id}")
    public Result<Void> approve(
            @PathVariable Long id,
            @RequestParam(required = false) String opinion) {
        log.info("审批请假记录，参数：id={}, opinion={}", id, opinion);
        // TODO: 获取当前登录用户ID
        Long auditorId = 1L;
        bizAskLeaveService.approve(id, opinion, auditorId);
        return Result.success();
    }

    @PutMapping("/reject/{id}")
    public Result<Void> reject(
            @PathVariable Long id,
            @RequestParam(required = false) String opinion) {
        log.info("驳回请假记录，参数：id={}, opinion={}", id, opinion);
        // TODO: 获取当前登录用户ID
        Long auditorId = 1L;
        bizAskLeaveService.reject(id, opinion, auditorId);
        return Result.success();
    }

    @PostMapping
    public Result<Void> submitLeave(@RequestBody BizAskLeave leave, HttpServletRequest request) {
        log.info("提交请假申请：{}", leave);
        // 获取当前登录用户信息
        Long userId = JwtUtils.getCurrentUserId(request);
        String employeeName = JwtUtils.getCurrentUsername(request);
        if (userId == null) {
            return Result.error("用户未登录");
        }

        // 设置申请人信息
        leave.setUserId(userId);
        leave.setEmployeeName(employeeName);
        leave.setStatus(0); // 设置初始状态为待审核
        leave.setCreateTime(new Date()); // 设置创建时间

        bizAskLeaveService.submitLeave(leave);
        return Result.success();
    }

    @GetMapping("/my")
    public Result<List<BizAskLeave>> getMyLeaves(HttpServletRequest request) {
        Long userId = JwtUtils.getCurrentUserId(request);
        if (userId == null) {
            return Result.error("用户未登录");
        }

        List<BizAskLeave> leaves = bizAskLeaveService.getLeavesByUserId(userId);
        return Result.success(leaves);
    }

    @PutMapping("/cancel/{id}")
    public Result<Void> cancelLeave(@PathVariable Long id, HttpServletRequest request) {
        log.info("撤销请假申请，id：{}", id);
        Long userId = JwtUtils.getCurrentUserId(request);
        if (userId == null) {
            return Result.error("用户未登录");
        }

        bizAskLeaveService.cancelLeave(id, userId);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<BizAskLeave> getLeaveDetail(@PathVariable Long id, HttpServletRequest request) {
        Long userId = JwtUtils.getCurrentUserId(request);
        if (userId == null) {
            return Result.error("用户未登录");
        }

        BizAskLeave leave = bizAskLeaveService.getLeaveById(id);
        return Result.success(leave);
    }


}